package com.m3839.sdk.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Description:DateUtils
 *
 * @author 陈宝阳
 * @create 2020/8/3 16:42
 */
public class DateUtils {

  /**
   * 用来判断当前是否还是在有效期内
   *
   * @throws ParseException
   */
  public static boolean isValidity(String day) {

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");

    // 当前的时刻
    Calendar pre = Calendar.getInstance();
    Date predate = new Date(System.currentTimeMillis());
    pre.setTime(predate);

    // 设定的时刻
    Calendar cal = Calendar.getInstance();
    Date date = null;
    try {
      date = sdf.parse(day);
    } catch (ParseException e) {
      e.printStackTrace();
    }
    cal.setTime(date);

    //用截止日期减去当前日期
    if (cal.get(Calendar.YEAR) == (pre.get(Calendar.YEAR))) {
      int diffDay = cal.get(Calendar.DAY_OF_YEAR) - pre.get(Calendar.DAY_OF_YEAR);
      int diffHour = cal.get(Calendar.HOUR_OF_DAY) - pre.get(Calendar.HOUR_OF_DAY);
      int diffMin = cal.get(Calendar.MINUTE) - pre.get(Calendar.MINUTE);
      if (diffDay == 0) {
        if (diffHour == 0) {
          if (diffMin > 5) {
            //有效期的截止时间提前五分钟
            return true;
          }
        } else if (diffHour > 0) {
          //说明还在有效期内
          return true;
        }
      } else if (diffDay > 0) {
        //说明还在有效期内
        return true;
      }
    } else if (cal.get(Calendar.YEAR) > (pre.get(Calendar.YEAR))) {
      //说明还在有效期内
      return true;
    }
    return false;
  }

  /**
   * 获取截止的时间（根据当前的时间加上天数）
   *
   * @param expireTime 时间单位是秒
   */
  public static String getDeadline(String expireTime) {
    //Date currentTime = new Date();
    //long newDateTime = currentTime.getTime() + 3600000 * 24 * days;
    Date newDate = new Date(Long.parseLong(expireTime) * 1000 - 15 * 60 * 1000);
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    String dateString = formatter.format(newDate);
    return dateString;
  }

  /**
   * 获取当前的时间
   * @return
   */
  public static String getCurrentTime() {
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date date = new Date(System.currentTimeMillis());
    return simpleDateFormat.format(date);
  }

}
